﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class MaintenanceWorkService : BaseService, IMaintenanceWorkService
    {
        private readonly IMaintenanceWorkRepository _maintenanceWorkRepository;
        private readonly IUnitOfWork unitOfWoxrk;
        public MaintenanceWorkService(IMapper mapper, IMaintenanceWorkRepository maintenanceWorkRepository, IUnitOfWork unitOfWoxrk) : base(mapper)
        {
            _maintenanceWorkRepository = maintenanceWorkRepository;
            this.unitOfWoxrk = unitOfWoxrk;
        }

        public bool AddOrModefiyMaintenanceWork(MaintenanceWork model, out string errorMsg)
        {
           
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                
                _maintenanceWorkRepository.UpdEntity(model);
            }
            else
            {
                _maintenanceWorkRepository.AddEntity(model);

            }
            return unitOfWoxrk.SaveChangesEntity();
        }

        public bool DelMaintenanceWork(long maintenanceWorkId)
        {
            var Quote = _maintenanceWorkRepository.QueryEntityById(maintenanceWorkId);
            if (Quote == null)
            {
                return false;
            }
            _maintenanceWorkRepository.DelEntity(Quote);
            return unitOfWoxrk.SaveChangesEntity();
        }

        public MaintenanceWork? GetMaintenanceWork(long maintenanceWorkId)
        {
            return _maintenanceWorkRepository.QueryEntityById(maintenanceWorkId);
        }

        public List<MaintenanceWork> GetMaintenanceWorkData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _maintenanceWorkRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.EquipmentName.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
    }
}
